package com.ai.module.websocket.config;

import com.ai.module.websocket.handler.DefaultWebSocketHandler;
import com.ai.module.websocket.handler.WebSocketInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private DefaultWebSocketHandler defaultWebSocketHandler;

    @Autowired
    private WebSocketInterceptor webSocketInterceptor;

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }


    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 链接方式如下 ws://127.0.0.1:9085/globalWs/message?token=qwncjncwqqdnjncz.adqwascsvcgrgb.cbrtbfvb
        // 如果你设置了springboot的 contentPath 那就需要在:9085端口后面
        // 加上contentPath 的值，在拼接上  globalWs/message?token=qwncjncwqqdnjncz.adqwascsvcgrgb.cbrtbfvb
        registry.addHandler(defaultWebSocketHandler,
                        "/websocket/aiProductChat")
                .addInterceptors(webSocketInterceptor)
                .setAllowedOrigins("*");
    }
}



